package com.maziro.spring.service.context;

import com.maziro.spring.app.AppConfig;
import com.maziro.spring.app.AppConfig2;
import com.maziro.spring.service.cycle.AService;
import com.maziro.spring.service.cycle.BService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ContextAutowired {
	public static void main(String[] args) {

		// Spring上下文环境
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//		context.register(UserService.class);
//		context.register(OrderService.class);
		context.register(AppConfig.class);
		context.refresh();

		// 子容器
		AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
		child.register(AppConfig2.class, AService.class, BService.class);
		child.setParent(context);
		child.refresh();

		AService aService = context.getBean(AService.class);
		System.out.println(aService);

		AService childBean = child.getBean(AService.class);
		System.out.println(childBean);

		// 父容器和子容器的 bean 不是同一个


		OrderService orderService = context.getBean(OrderService.class);
		String order = orderService.getOrder(123L);
		System.out.println(order);
	}

}
